#!/usr/bin/perl -w

#=======================================================================
# create_new
# File ID: 5dad39ac-f742-11dd-a5bf-000475e441b9
# Creates various pristine setups for all kind of things — documents, 
# source code and so on.
#
# Character set: UTF-8
# ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;

$| = 1;

our $Debug = 0;

our %Opt = (

    'debug' => 0,
    'help' => 0,
    'no-svn' => 0,
    'verbose' => 0,
    'version' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

my $CMD_SVN = "svn";
my $CMD_STD = "$ENV{'HOME'}/bin/std";
my $CMD_KEYW = "$ENV{'HOME'}/bin/keyw";

Getopt::Long::Configure("bundling");
GetOptions(

    "debug" => \$Opt{'debug'},
    "help|h" => \$Opt{'help'},
    "no-svn|n" => \$Opt{'no-svn'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

if (!defined($ARGV[1])) {
    die("$progname: Not enough parameters, use -h for help.\n");
}

my ($Type, $Name) = (shift, shift);

if ($Type =~ /^article$/) {
    mkdir($Name) || die("$progname: mkdir $Name: $!\n");
    system("$CMD_STD db-article >$Name/$Name.xml");
    if (open(OutFP, ">", "$Name/Makefile")) {
        my $svn_str_update = !$Opt{'no-svn'} && "\tsvn update \$(HTML_TARGET)\n" || "";
        print(OutFP <<END);
#!/usr/bin/make

# \$Id\$

SHELL = /bin/bash
XSLTPROC = xsltproc
XSLTOPTS =
XHTML_XSL = \$(HOME)/xsl/xhtml/docbook.xsl
HTML_TARGET = $Name.html

all:
\t\$(MAKE) \$(HTML_TARGET)

\$(HTML_TARGET): $Name.xml
\t\$(XSLTPROC) -o \$(HTML_TARGET) \$(XSLTOPTS) \$(XHTML_XSL) $Name.xml
\txmllint --format --encode UTF-8 \$(HTML_TARGET) >$Name.tmp
\tmv $Name.tmp \$(HTML_TARGET)

check:
\t\@(for _f in *.xml; do \\
\t\techo ==== \$\$_f ====; \\
\t\txmllint --valid --noout \$\$_f; \\
\t\tdone \\
\t)

htmlclean:
\trm -f \$(HTML_TARGET)
$svn_str_update
clean:
\t\$(MAKE) htmlclean

valid:
\t\$(MAKE) check
END
        close(OutFP);
    } else {
        warn("$progname: $Name/Makefile: Cannot create file: $!\n");
    }
    $Opt{'no-svn'} || my_system($CMD_SVN, "add", $Name);
    $Opt{'no-svn'} || my_system($CMD_KEYW, "$Name/Makefile", "$Name/$Name.xml");
} else {
    die("$progname: $Type: Unknown type\n");
}

sub my_system {
    my @Cmd = @_;
    my $cmd_txt = join(" ", @Cmd);
    print("======== $progname: Executing \"$cmd_txt\"... ========\n");
    system(@Cmd);
    return;
}

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Usage: $progname type directory

Options:

  -h, --help
    Show this help.
  -n, --no-svn
    Don’t use Subversion commands (svn add, etc)
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

Supported types:

  article
    DocBook article.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
